<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 文章管理
 */
class BannerController extends BaseController
{
    /**
     * banner列表
     * @return [type] [description]
     */
    public function index()
    {
        $model = D("bc_banner");
        $data=$model->select();
        $this->assign('data', $data);
        $this->display();     
    }
    /**
     * 添加banner
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("bc_banner");
            // $_POST['add_time']=time();
            // $model->add_time = time();
             $_POST['add_time']=time();
            $data = [];
            $data['title']="'".$_POST["title"]."'";
            $data['add_time']="'".$_POST["add_time"]."'";
            $data['introduce']="'".$_POST["introduce"]."'";
           
            $data['img_url']="'".$_POST["img_url"]."'";
            
            $sql = "insert into  bc_banner(introduce,title,add_time,img_url) value(".$data['introduce'].",".$data['title'].",".$data['add_time'].",".$data['img_url'].")";
            $b=$model->execute($sql);


            if ($b) {
                $this->success("添加成功", U('banner/index'));
            } else {
                $this->error("添加失败");
            }

        }
    }

    /**
     * 删除banner
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $model =D("bc_banner");
        $result = $model->where("banner_id=".$id)->delete();
        if($result){
            $this->success("删除成功", U('banner/index'));
        }else{
            $this->error("删除失败");
        }
    }

    public function update($id){
        if(!IS_POST){
            $model =D("bc_banner");
            $data=$model->where(['banner_id'=>$id])->find();
            $this->assign('data',$data);
            $this->display();
        }
        if(IS_POST){
            $model =D("bc_banner");
            $_POST['add_time']=time();
            $data = [];
            $data['title']="'".$_POST["title"]."'";
            $data['add_time']="'".$_POST["add_time"]."'";
            $data['introduce']="'".$_POST["introduce"]."'";
           
            $data['img_url']="'".$_POST["img_url"]."'";
            
            $sql = "update bc_banner set introduce=".$data['introduce'].",title=".$data['title'].",add_time=".$data['add_time'].",img_url=".$data['img_url']." where banner_id = $id";
            $b=$model->execute($sql);
            
            if ($b) {
                $this->success("修改成功", U('banner/index'));
            } else {
                $this->error("修改失败");
            }
        }
    }

}
